package com.backend.vse.ws;

import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * WebSocket 控制器，用于处理实验相关操作。
 * 该控制器提供匹配用户和开始实验的端点。
 */
@Slf4j
@RestController
@RequestMapping("/exp8")
public class AIsellerController {

    private static AIseller AIseller=new AIseller();

    private Random random = new Random();
    @PostMapping("/seller/makePrice")
    public Map<String, Object> getSellerOffer()
    {
        //AIseller = new AIseller();

        int highPrice = AIseller.getMaxPrice();
        int lowPrice = AIseller.getMinPrice();
        String situation = AIseller.getSituation();

        // 创建 Map 对象，存储 highPrice, lowPrice 和 situation
        Map<String, Object> result = new HashMap<>();
        result.put("highPrice", highPrice);
        result.put("lowPrice", lowPrice);
        result.put("situation", situation);
        return result;
    }
    @GetMapping("/buyer/offer")
    public Map<String, Object> handleBuyerOffer(@RequestParam String purchase, @RequestParam int offerPrice,@RequestParam double sale) {
        // 处理买方的出价和是否购买的逻辑
        // 这里可以加入你的业务逻辑，例如验证出价是否在合理范围内，是否接受购买等
        boolean canSale = true;
        AIseller.setBuyerPrice(offerPrice);
        AIseller.setPurchase(purchase);

        if(purchase.equals('2')){
            AIseller.setAlterPrice(sale-500);
            if(offerPrice<AIseller.getMinPrice()){
                canSale=false;
            }

        }
        AIseller.setAlterPrice(sale-500);
        Map<String, Object> result = new HashMap<>();
        result.put("purchase", purchase);
        result.put("offerPrice", offerPrice);
        result.put("sale",AIseller.getAlterPrice());
        result.put("canSale",canSale);
        // 假设你还有一些额外的处理结果需要返回
        result.put("message", "Offer processed successfully");
        return result;
    }
    //111
    @GetMapping("/buyer/negotiate")
    public Map<String, Object> handleSubmitPrice(@RequestParam int offerPrice) {
        boolean canSale = true;
        if(offerPrice<AIseller.getMinPrice()){
            canSale=false;
        }
        int decrement = random.nextInt(301) + 300;
        double current =AIseller.getAlterPrice();
        // 返回结果
        AIseller.setAlterPrice(current-decrement);
        Map<String, Object> result = new HashMap<>();
        result.put("sellprice",AIseller.getAlterPrice());
        result.put("message", "Price updated successfully");

        return result;
    }
}
